package com.taotao.service.com.taotao.service.Impl;

import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.service.ItemParamItemService;
import com.taotao.uitls.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mafuhua on 2017/9/20.
 */
@Service
public class ItemParamItemServiceImpl implements ItemParamItemService {
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;


    @Override
    public String getItemParemById(long itemId) {
        TbItemParamItem tbItemParamItem;

        // tbItemParam = tbItemParamMapper.selectByPrimaryKey(cid);
        TbItemParamItemExample example = new TbItemParamItemExample();
        //创建查询条件
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> tbItemParamItems = tbItemParamItemMapper.selectByExampleWithBLOBs(example);

        //判断list中是否为空
        if (tbItemParamItems != null && tbItemParamItems.size() > 0) {
            tbItemParamItem = tbItemParamItems.get(0);
            String paramData = tbItemParamItem.getParamData();
            List<Map> mapList = JsonUtils.jsonToList(paramData, Map.class);
            //遍历list生成html
            StringBuffer sb = new StringBuffer();

            sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"1\" class=\"Ptable\">\n");
            sb.append("	<tbody>\n");
            for (Map map :
                    mapList) {
                sb.append("		<tr>\n");
                sb.append("			<th class=\"tdTitle\" colspan=\"2\">" + map.get("group") + "</th>\n");
                sb.append("		</tr>\n");
                //取规格项
                List<Map> mapList2 = (List<Map>) map.get("params");
                for (Map map2 :
                        mapList2) {
                    sb.append("		<tr>\n");
                    sb.append("			<td class=\"tdTitle\">" + map2.get("k") + "</td>\n");
                    sb.append("			<td>" + map2.get("v") + "</td>\n");
                    sb.append("		</tr>\n");
                }
            }
            sb.append("	</tbody>\n");
            sb.append("</table>");

            return sb.toString();

        }

        return "";
    }
}
